<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\form\admin\model;

use think\Model;
use app\admin\model\User;

class Message extends Model
{
    protected $name = 'form_message';
    
    // 设置json类型字段
    protected $json = ['content'];

    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    // 关联模型
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    // 搜索器
    public function searchKeywordAttr($query, $value, $array)
    {
    	if (! empty($value)) {
	        $query->where("content|ip|country|province|city",'like', '%' . $value . '%');
	    }
    }

    public function searchDateAttr($query, $value, $array)
    {
        if (! empty($value)) { 
            $query->whereBetweenTime('create_time', $value[0], $value[1]);
        }
    }

    public function searchIdAttr($query, $value, $array)
    {
        if (! empty($value)) {
            $query->where("form_id", $value);
        }
    }

    public function getContentAttr($value, $array)
    {
        $arr = [];
        foreach ($value as $k => $v) {
            $arr[$k] = $v;
        }
        return $arr;
    }
}